<!--  -->
<template>
  <el-table :data="BrandListTableData" border style="width: 100%">
    <el-table-column align="center" type="selection"> </el-table-column>
    <el-table-column
      align="center"
      prop="id"
      size="small"
      label="编号"
      width="100"
    >
    </el-table-column>
    <el-table-column align="center" prop="name" label="品牌名称" width="250">
    </el-table-column>
    <el-table-column
      align="center"
      prop="firstLetter"
      label="品牌首字母"
      width="100"
    >
    </el-table-column>
    <el-table-column align="center" prop="sort" label="排序" width="100">
    </el-table-column>
    <el-table-column align="center" label="品牌制造商">
      <template #default="{ row }">
        <div>
          <el-switch
            v-model="row.factoryStatus"
            :active-value="1"
            :inactive-value="0"
            @change="changeFactoryStatus(row.factoryStatus, row.id)"
          />
        </div>
      </template>
    </el-table-column>
    <el-table-column prop="sort" align="center" label="是否显示">
      <template #default="{ row }">
        <div>
          <el-switch
            v-model="row.showStatus"
            :active-value="1"
            :inactive-value="0"
            @change="changeShowStatus(row.showStatus, row.id)"
          />
        </div>
      </template>
    </el-table-column>
    <el-table-column align="center" label="相关" width="250">
      <template #default="{ row }">
        <div class="relevant">
          <span>商品：<el-button type="primary" link>100</el-button></span>
          <span>评价：<el-button type="primary" link>1000</el-button></span>
        </div>
      </template>
    </el-table-column>

    <el-table-column align="center" label="操作">
      <template #default="{ row }">
        <div class="brandHandle">
          <p><el-button size="small">编辑</el-button></p>
          <p>
            <el-button size="small" type="danger" @click="removeGoods(row.id)"
              >删除</el-button
            >
          </p>
        </div>
      </template>
    </el-table-column>
  </el-table>

  <!-- 下拉 -->
  <div class="goods-select">
    <el-select v-model="value" class="select" placeholder="Select" size="large">
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
    </el-select>
    <el-button type="primary" size="large">确定</el-button>
  </div>

  <div class="sorter">
    <!-- 分页  -->
    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>

  <!-- 点击删除 -->
  <deleteGood :showDelGoods="showDelGoods" @hidden="hiddenDel"></deleteGood>
</template>

<script lang="ts" setup>
import { reactive, toRefs, ref } from "vue";
import * as http from "../../request/http";
import { Edit } from "@element-plus/icons-vue";
import deleteGood from "../pms/components/productDel.vue";
import { ElMessage } from "element-plus";

// toast提示框
const openToast = () => {
  ElMessage({
    message: "修改成功",
    type: "success",
  });
};

// 下拉框批量选择
const value = ref("批量操作");
const options = [
  {
    value: 1,
    label: "显示品牌",
  },
  {
    value: 0,
    label: "隐藏品牌",
  },
];
// 获取商品页数据
const BrandListTableData = ref([]);
async function fetchData(
  keyword: string = "",
  pageNum: number = 1,
  pageSize: number = 10
) {
  let res = await http.getBrandList({
    keyword,
    pageNum,
    pageSize,
  });
  if (res.code === 200) {
    // console.log(res);
    BrandListTableData.value = res.data.list;
    // 总条数
    total.value = res.data.total;
  }
}
fetchData();

// 分页 组件
let currentPage = ref(1);
let pageSize = ref(10);
let total = ref(0);

// 一页几条 条码
const handleSizeChange = (size: number) => {
  // console.log(size);
  fetchData("", currentPage.value, size);
};
// 页码
const handleCurrentChange = (curPage: number) => {
  fetchData("", curPage, pageSize.value);
};

// 点击删除
let showDelGoods = ref(false);
let ids = ref(0);

function removeGoods(id: number) {
  // console.log("这是id", id);
  // console.log("这是del", delStatus);
  showDelGoods.value = true;
  ids.value = id;
}
// 子组件点击取消 隐藏删除弹框
async function hiddenDel(flag?: string) {
  showDelGoods.value = false;
  if (flag === "reloadDel") {
    let res = await http.getBrandDEL(ids.value);
    // console.log("删除", res);
    if (res.code === 200) {
      fetchData("", currentPage.value, pageSize.value);
    }
  }
}

// 点击滑块修改品牌制造商状态
async function changeFactoryStatus(val: number, id: number) {
  // console.log(val,id);
  let res = await http.updateFactoryStatus({
    ids: id,
    factoryStatus: val,
  });
  if (res.code === 200) {
    openToast();
  }
}

// 修改显示状态
async function changeShowStatus(val: number, id: number) {
  // console.log(val,id);
  let res = await http.updateShowStatus({
    ids: id,
    showStatus: val,
  });
  if (res.code === 200) {
    openToast();
  }
}
</script>
<style lang="less" scoped global>
.sorter {
  float: right;
  margin-top: 10px;
  margin-right: 10px;
}

.goods-select {
  float: left;
  margin-top: 10px;
  margin-left: 10px;
  .select {
    margin-right: 10px;
  }
}

.brandHandle {
  display: flex;
  justify-content: center;
  :nth-child(1) {
    margin-right: 5px;
  }
}

.relevant {
  span {
    padding: 0 10px;
    box-sizing: border-box;
  }
}
</style>
